﻿using BuzzwordBingo.Interface;
using NUnit.Framework;

namespace BuzzwordBingo.Entities.Test
{
    /// <summary>
    /// Summary description for BingoCardTest
    /// </summary>
    [TestFixture]
    public class BingoCardFixture
    {
        IBingoCard _card;

        public BingoCardFixture()
        {
            _card = new BingoCard();
        }

        [SetUp]
        public void Setup()
        {
            _card = new BingoCard();
            InitializeBingocard();
        }

        [TearDown]
        public void Teardown()
        {
            _card = null;
        }

        [Test]
        public void MarkBuzzWord()
        {
            _card.MarkBuzzWord(7);
            IBuzzWord word = _card.BuzzWords[2, 1];
            Assert.IsTrue(word.IsMarked);
        }

        [Test]
        public void CheckCompletedAcross()
        {
            for (int k = 0; k < 5; k++)
            {
                for (int j = 0; j < 5; j++)
                {
                    _card.MarkBuzzWord(j + k * 5);
                }
                _card.CheckCompleted();
                Assert.IsTrue(_card.IsComplete);
                InitializeBingocard();
            }
        }

        [Test]
        public void CheckCompletedDown()
        {
            for (int j = 0; j < 5; j++)
            {
                for (int k = 0; k < 5; k++)
                {
                    _card.MarkBuzzWord(j + k * 5);
                }
                _card.CheckCompleted();
                Assert.IsTrue(_card.IsComplete);
                InitializeBingocard();
            }
        }

        [Test]
        public void CheckCompletedDiagonals()
        {
            for (int i = 0; i < 5; i++)
                _card.MarkBuzzWord(i + i * 5);

            _card.CheckCompleted();
            Assert.IsTrue(_card.IsComplete);

            InitializeBingocard();
            for (int i = 0; i < 5; i++)
                _card.MarkBuzzWord(4 - i + i * 5);

            _card.CheckCompleted();
            Assert.IsTrue(_card.IsComplete);
        }

        #region factory methods

        private void InitializeBingocard()
        {
            int j, k;
            for (int i = 0; i < 25; i++)
            {
                j = i % 5;
                k = i / 5;
                _card.BuzzWords[j, k] = new BuzzWord(i.ToString());
            }
        }

        #endregion
    }
}
